#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <utmp.h>
#include <unistd.h>

void show(struct utmp* utmp_buffer){
	if(utmp_buffer->ut_type==USER_PROCESS){
		printf("%s", utmp_buffer->ut_user);
		printf("\t");
		printf("%s", utmp_buffer->ut_line);
		printf("\t");
		printf("%ld", utmp_buffer->ut_tv.tv_sec);
		printf("(");
		printf("%s",utmp_buffer->ut_host);
		printf(")");
		printf("\n");
	}
}

int main(){
	struct utmp current_record;
	int utmpd;
	int reclen;

	if(utmpd=open("/var/run/utmp", O_RDONLY)==-1){
		printf("errors in opening var/run/utmp\n");
		exit(0);
	}
	reclen=sizeof(current_record);

	while(read(utmpd, &current_record,reclen)==reclen){
		show(&current_record);
	}
	close(utmpd);
	return 0;
		
}



